package com.xunua.mymycsjsdktest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTDrawFeedAd;
import com.bytedance.sdk.openadsdk.TTNativeAd;

import java.util.ArrayList;
import java.util.List;

public class TTDrawFeedActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_t_t_draw_feed);

        TTAdManager adManager = TTAdSdk.getAdManager();
        adManager.requestPermissionIfNecessary(this);
        TTAdNative adNative = adManager.createAdNative(this);

        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId("901121709")
                .setSupportDeepLink(true)
                .setImageAcceptedSize(1080, 1920)
                .setAdCount(1)
                .build();
        adNative.loadDrawFeedAd(adSlot, new TTAdNative.DrawFeedAdListener() {
            @Override
            public void onError(int i, String s) {
                Toast.makeText(TTDrawFeedActivity.this, i+"-----"+s, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDrawFeedAdLoad(List<TTDrawFeedAd> list) {
                if (list==null||list.isEmpty()){
                    Toast.makeText(TTDrawFeedActivity.this, "ad is null", Toast.LENGTH_SHORT).show();
                    return;
                }
                //设置activity对象，为下载相关的行为提供context 上下文
                for (TTDrawFeedAd ad:
                        list) {
                    ad.setActivityForDownloadApp(TTDrawFeedActivity.this);
                    //点击事件监听必须在getAdView之前调用
                    ad.setDrawVideoListener(new TTDrawFeedAd.DrawVideoListener() {
                        @Override
                        public void onClickRetry() {
                            Toast.makeText(TTDrawFeedActivity.this, "单击重试", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onClick() {
                            Toast.makeText(TTDrawFeedActivity.this, "点击下载", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
                TTDrawFeedAd ttDrawFeedAd = list.get(0);
                //设置广告视频区域是否响应点击事件，控制视频暂停、继续播放、  默认是不响应
                ttDrawFeedAd.setCanInterruptVideoPlay(true);
                //设置视频暂停的icon和大小
                ttDrawFeedAd.setPauseIcon(BitmapFactory.decodeResource(getResources(),R.drawable.tt_dislike_icon),60);
                //获取广告视频播放的view并放入广告容器中
                FrameLayout frameLayout=findViewById(R.id.framelayout_TTDrawFeedAd);
                frameLayout.removeAllViews();
                frameLayout.addView(ttDrawFeedAd.getAdView());
                //初始化并绑定广告行为
                initAdViewAndAction(ttDrawFeedAd,frameLayout);
            }
        });
    }
    private void initAdViewAndAction(TTDrawFeedAd ad, FrameLayout container) {
        View actionView = this.getLayoutInflater().inflate(R.layout.native_draw_action_item,null,false);
        TextView title = actionView.findViewById(R.id.tv_title);
        title.setText(ad.getTitle());

        TextView desc = actionView.findViewById(R.id.tv_desc);
        desc.setText(ad.getDescription());

        Button action = actionView.findViewById(R.id.button_creative);
        action.setText(ad.getButtonText());

        int margin = (int) dip2Px(this, 10);
        FrameLayout.LayoutParams lp1 = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp1.gravity = Gravity.START | Gravity.BOTTOM;
        lp1.leftMargin = margin;
        lp1.bottomMargin = margin;
        container.addView(actionView, lp1);

        List<View> clickViews = new ArrayList<>();
        clickViews.add(title);
        clickViews.add(desc);
        List<View> creativeViews = new ArrayList<>();
        creativeViews.add(action);
        ad.registerViewForInteraction(container, clickViews, creativeViews, new TTNativeAd.AdInteractionListener() {
            @Override
            public void onAdClicked(View view, TTNativeAd ad) {
//                showToast("onAdClicked");
                Toast.makeText(TTDrawFeedActivity.this, "点击了广告 onAdClicked", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdCreativeClick(View view, TTNativeAd ad) {
//                showToast("onAdCreativeClick");
                Toast.makeText(TTDrawFeedActivity.this, "点击了下载按钮或者查看详情按钮 onAdCreativeClick", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdShow(TTNativeAd ad) {
//                showToast("onAdShow");
//                Toast.makeText(TTDrawFeedActivity.this, "广告展示了", Toast.LENGTH_SHORT).show();
            }
        });
    }
    private float dip2Px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return dipValue * scale + 0.5f;
    }
}